package com.hunan.api.catv.action.guessing;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.annotation.MemberAuth;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.service.biz.guessing.GuessingBiz;
import com.hunan.api.catv.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/6/3 0003
 * @package com.hunan.api.catv.action.guessing
 * @company 湖南有线
 */
@RestController
@RequestMapping(Api.guessing)
public class GuessingAction extends BaseAction {

    @Autowired
    GuessingBiz guessingBiz;
    @Autowired
    RedisUtils redisUtils;


    @GetMapping("/europeanCup/ranking/list")
    public Result rankingPrizeList() {
        return buildOK(guessingBiz.rankingPrizeList(), "");
    }

    @GetMapping("/europeanCup/myRanking")
    @MemberAuth
    public Result myRanking(HttpServletRequest request) {
        return buildOK(guessingBiz.myRankingWithDraws(getMemberId(request)), "");
    }

    @GetMapping("/europeanCup/matchItem/list")
    public Result matchItemList(HttpServletRequest request) {
        return buildOK(guessingBiz.matchItemList(getMemberId(request)), "");
    }

    @MemberAuth
    @PostMapping("/europeanCup/match/reservation")
    public Result reservationMatch(Integer matchItemId, HttpServletRequest request) {
        Integer memberId = getMemberId(request);
        log.info("欧洲杯预约接口，参数：[{}]-[{}]-[{}]", matchItemId, memberId, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHssmm")));
        Boolean isNotRequest = redisUtils.setNX("guessing_reservation_lock_" + matchItemId + "-" + memberId, "1", 2, TimeUnit.SECONDS);
        if (!isNotRequest) {
            return buildFail("", "操作太频繁");
        }
        if (ObjectUtils.isEmpty(matchItemId)) {
            return buildRequire("matchItemId is required");
        }
        return buildOK(guessingBiz.reservationMatch(matchItemId, memberId), "");
    }

    @MemberAuth
    @PostMapping("/europeanCup/match/guessing")
    public Result guessingMatch(Integer matchItemId, HttpServletRequest request, Integer guessingMatchResult, String guessingTeam) {
        Integer memberId = getMemberId(request);
        log.info("欧洲杯竞猜接口，参数：[{}]-[{}]-[{}]-[{}]-[{}]", matchItemId, memberId, guessingMatchResult, guessingTeam, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHssmm")));
        Boolean isNotRequest = redisUtils.setNX("guessing_match_lock_" + matchItemId + "-" + memberId, "1", 3, TimeUnit.SECONDS);
        if (!isNotRequest) {
            return buildFail("", "操作太频繁");
        }
        if (ObjectUtils.isEmpty(matchItemId)) {
            return buildRequire("matchItemId is required");
        }
        if (ObjectUtils.isEmpty(guessingMatchResult)) {
            return buildRequire("guessingMatchResult is required");
        }
        if (StringUtils.isBlank(guessingTeam)) {
            return buildRequire("guessingTeam is required");
        }
        return buildOK(guessingBiz.guessingMatch(matchItemId, guessingMatchResult, guessingTeam, memberId), "");
    }

    @MemberAuth
    @GetMapping("/europeanCup/member/guessingInfo")
    public Result guessingInfo(Integer matchItemId, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(matchItemId)) {
            return buildRequire("matchItemId is required");
        }
        return buildOK(guessingBiz.guessingInfo(matchItemId, getMemberId(request)), "");
    }

}
